package Graphique;

import java.awt.Color;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;

import Command.Command;
import Command.CommandNext;
import Command.CommandPrev;
import Command.CommandReset;
import Command.CommandSave;
import Command.CommandSearch;
import CommandMenu.CommandDetail;
import CommandMenu.CommandMenu;
import Connexion.ChargeurdeDonnees;
import Connexion.ConnectionFactory;
import Connexion.mysqlConnector;
import Listener.ButtonListener;
import Listener.ListListener;

public class MainFrame {

	private Display display;
	private Shell shell;
	private CommandMenu commandDetail;
	private CTabFolder tabFolder;
	private CTabItem filmTab, castingTab;
	private MoviePointerTabOne compositeFilmTab;
	private ConnectionFactory connection;
	private CompositeList compositelistFilms;
	private Sash sash;
	private FormData sashData, tabFolderData;

	public MainFrame(ConnectionFactory connection) {
		this.connection = connection;
		initShell();
		initFormData();
		initMenus();
		initCompositeItems();
		initTabFolders();
		shell.open();

		// Set up the event loop.
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				// If no more entries in event queue
				display.sleep();
			}
		}

		display.dispose();

	}

	/**
	 * @return the connection
	 */
	public ConnectionFactory getFactory() {
		return connection;
	}

	private void initTabFolders() {

		tabFolder = new CTabFolder(shell, SWT.NONE);
		
		filmTab = new CTabItem(tabFolder, SWT.BORDER);
		filmTab.setText("Le film");

		castingTab = new CTabItem(tabFolder, SWT.BORDER);
		castingTab.setText("Casting et notes de critiques");
		
		compositeFilmTab = new MoviePointerTabOne(tabFolder, SWT.NONE);
		filmTab.setControl(compositeFilmTab);
		compositeFilmTab.setMainFrame(this);
				
		tabFolder.setLayoutData(tabFolderData);
		tabFolder.setSelection(0);
		compositeFilmTab.setListReferences();

	}

	private void initCompositeItems() {
				
		compositelistFilms = new CompositeList(shell, SWT.NONE);
		compositelistFilms.setSash(sash);
		compositelistFilms.setToolTipText("liste films");
		compositelistFilms.initList(connection);
		sash.setLayoutData (sashData);
		compositelistFilms.initExpandSearch();
		compositelistFilms.setMainFrame(this);
	}

	private void initFormData() {		
		
		sash = new Sash(shell, SWT.VERTICAL);

		sashData = new FormData();
		sashData.left = new FormAttachment(30, 0);
		sashData.top = new FormAttachment(0, 0);
		sashData.bottom = new FormAttachment(100, 0);
		
		
		tabFolderData = new FormData();
		tabFolderData.left = new FormAttachment (sash, 0);
		tabFolderData.right = new FormAttachment (100, 0);
		tabFolderData.top = new FormAttachment (0, 0);
		tabFolderData.bottom = new FormAttachment (100, 0);		

	}

	private void initShell() {

		display = new Display();
		shell = new Shell(display, SWT.CLOSE | SWT.CENTER);
		shell.setLayout(new FormLayout());
		shell.setText("Referencement de films");

	}

	private void initMenus() {

		Menu menu = new Menu(shell, SWT.BAR | SWT.BORDER);
		MenuItem file = new MenuItem(menu, SWT.CASCADE);
		file.setText("File");

		Menu filemenu = new Menu(shell, SWT.DROP_DOWN);
		file.setMenu(filemenu);

		
		commandDetail = new CommandDetail(connection);
		MenuItem detailItem = new MenuItem(filemenu, SWT.PUSH);
		detailItem.setText("Détails");
		detailItem.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event arg0) {
				commandDetail.execute();
			}

		});

		MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
		exitItem.setText("Exit");
		exitItem.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event arg0) {
				shell.dispose();
			}

		});

		shell.setMenuBar(menu);

	}

	public MoviePointerTabOne getFilmTab(){
		return this.compositeFilmTab;
	}
	
	public List getList(){
		return compositelistFilms.getList();
	}
	
	
}